
/* STANDARD INCLUDE FILES */
{mfdeclre.i}
{gplabel.i} /* EXTERNAL LABEL INCLUDE */
{pxmaint.i}
{pxsevcon.i}

/* SHARED FRAMES */
define shared frame chead.
define shared frame c.
define shared frame d.

/* SHARED VARIABLES */
define shared variable desc1 like pt_desc1.
define shared variable desc2 like pt_desc2.
define shared variable line like sod_line.
define shared variable po_recno as recid.
define shared variable ext_cost like pod_pur_cost.
define shared variable pod_recno as recid.
define shared variable podcmmts       like mfc_logical.
define shared variable sngl_ln like poc_ln_fmt.
define shared variable mfgr like vp_mfgr.
define shared variable mfgr_part like vp_mfgr_part.
define shared variable continue like mfc_logical.
define shared variable st_qty like pod_qty_ord.
define shared variable st_um like pod_um.
define shared variable clines as integer initial ?.
define shared variable blanket   like mfc_logical.
define shared variable podnbr like pod_nbr.
define shared variable podline like pod_line.
define shared variable old_pod_site like pod_site.
define shared variable line_opened as logical.
define shared variable new_pod like mfc_logical.

/* LOCAL VARIABLES */
define variable old_db      like si_db       no-undo.
define variable old_site_db like si_db       no-undo.
define variable new_site_db like si_db       no-undo.
define variable err-flag    as   integer     no-undo.
define variable yn          like mfc_logical no-undo.
define variable reqerr      as   logical     no-undo.
define variable errcd       as   integer     no-undo.
define variable using_grs   like mfc_logical no-undo.

if c-application-mode = "API" then do on error undo,return:
    /* COMMON API CONSTANTS AND VARIABLES */
    {mfaimfg.i}
    /* PURCHASE ORDER API TEMP-TABLE, NAMED USING THE "Api" PREFIX */
    {popoit01.i}
    /* GET HANDLE OF API CONTROLLER */
    {gprun.i ""gpaigh.p""
        "(output ApiMethodHandle,
        output ApiProgramName,
        output ApiMethodName,
        output apiContextString)"}
        
    if global-beam-me-up then undo, leave.
/* GET LOCAL PO DETAIL TEMP-TABLE */
    create ttPurchaseOrderDet.
    run getPurchaseOrderDetRecord in ApiMethodHandle (buffer ttPurchaseOrderDet).
end.  /* If c-application-mode = "API" */

/* DEFINES FORMS c & d */
{mfpomtb.i}

using_grs = can-find(mfc_ctrl where mfc_field   = "grs_installed" and mfc_logical = yes).

/*============================================================================*/
/* ****************************** Main Block ******************************** */
/*============================================================================*/

if sngl_ln then clines = 1.

continue = no.

for first po_mstr fields(po_nbr po_vend) where recid(po_mstr) = po_recno no-lock:
end.

for first pod_det where recid(pod_det) = pod_recno exclusive-lock:
end.

setreq:
do on error undo, retry:
    if global-beam-me-up then undo, leave.

/* DO NOT RETRY WHEN PROCESSING API REQUEST */
    if retry and c-application-mode = "API" then
        undo setreq, return error.
    ststatus = stline[1].
    status input ststatus.

    if  c-application-mode <> "API" then
        assign pod_site.
    else /* If c-application-mode = "API" */
        assign {mfaiset.i pod_site ttPurchaseOrderDet.site}.

    pod_recno = recid(pod_det).

    {pxrun.i &PROC='validateSite' &PROGRAM='icsixr.p'
        &PARAM="(input ""pod_det.pod_site"",
        input pod_site,
        input no,
        input '')"
        &NOAPPERROR=True
        &CATCHERROR=True}

    if return-value <> {&SUCCESS-RESULT} then do:
        undo setreq, return error.
    end.

    {pxrun.i &PROC='validateSiteSecurity' &PROGRAM='icsixr.p'
        &PARAM="(input pod_site,
        input '')"
        &NOAPPERROR=True
        &CATCHERROR=True}

    if return-value <> {&SUCCESS-RESULT} then do:
        undo setreq, return error.
    end.

    if not new_pod then do:
        if pod_site <> old_pod_site then do:
            if (pod_status = "C" or pod_status = "X") then do:
                {pxrun.i &PROC='reOpenPurchaseOrder' &PROGRAM='popoxr.p'
                    &PROGRAM='popoxr.p'
                    &PARAM="(input pod_nbr)"
                    &NOAPPERROR=True &CATCHERROR=True}

                assign
                    pod_status = ""
                    line_opened = true.
            end.

            {pxrun.i &PROC='getSiteDataBase' &PROGRAM='icsixr.p'
                &PARAM="(input old_pod_site,
                output old_site_db)"
                &NOAPPERROR=True
                &CATCHERROR=True}

            if pod_blanket <> "" then do:
                {pxrun.i &PROC='getSiteDataBase' &PROGRAM='icsixr.p'
                    &PARAM="(input pod_site,
                    output new_site_db)"
                    &NOAPPERROR=True
                    &CATCHERROR=True}

                if new_site_db <> old_site_db then do:
                    undo setreq, return error.
                end.
            end. /* IF pod_blanket <> "" */

            if pod_req_nbr <> "" then do:
                reqerr = false.
                {gprun.i ""popomtia.p""
                    "(input yes,
                    input pod_site,
                    input pod_req_nbr,
                    input pod_req_line,
                    input old_site_db,
                    input using_grs,
                    output reqerr)"}

                if global-beam-me-up then undo, leave.

                if reqerr then do:
                    undo setreq, return error.
                end.
            end. /* POD_REQ_NBR <> "" */

            if old_site_db <> global_db then do:
                assign
                    old_db = global_db
                    podnbr  = pod_nbr
                    podline = pod_line.

                {gprun.i ""gpalias3.p"" "(old_site_db, output err-flag)"}
                if global-beam-me-up then undo, leave.

                {gprun.i ""popomta2.p""}
                if global-beam-me-up then undo, leave.

                {gprun.i ""gpalias3.p"" "(old_db, output err-flag)" }
                if global-beam-me-up then undo, leave.
            end. /*IF OLD_SITE_DB <> GLOBAL_DB */

            {pxrun.i &PROC='deleteMRPDetailForPOLine' &PROGRAM='popoxr1.p'
                &PARAM="(input pod_part,
                input pod_nbr,
                input string(pod_line))"
                &NOAPPERROR=True &CATCHERROR=True}

            {gprun.i ""popomteb.p""}
            if global-beam-me-up then undo, leave.

            {pxrun.i &PROC='getERSLineOption' &PROGRAM='aperxr.p'
                &PARAM="(input po_ers_opt,
                input po_pr_lst_tp,
                input po_vend,
                input pod_part,
                input pod_site,
                output pod_ers_opt,
                output pod_pr_lst_tp)"
                &NOAPPERROR=True
                &CATCHERROR=True}
        end. /* IF POD_SITE <> OLD_POD_SITE */
    end. /* IF NOT NEW pod_det */
    continue = yes.
end.
if global-beam-me-up then undo, leave.
